//
// Created by root on 10/5/15.
//
#include "common.h"
#include "http_deal.h"
#include <signal.h>
#include <request.h>
#include <my_functon.h>
#include <response.h>
#include "socket_thread_map.h"

//global thread socket map
socket_thread_map *socket_thread_table;

void pipe_handler(int sig){
    socket_thread_map * socket_thread_map1 = find_socket_thread(&socket_thread_table,pthread_self());
    close(atoi(socket_thread_map1->socketid));
    delete_socket_thread(&socket_thread_table,pthread_self());
    pthread_exit("close cur thread\n");
}

void* http_deal(void* pAcceptFd) {

    struct sigaction sa;
    sa.sa_handler = pipe_handler;
    sa.sa_flags = 0;
    sigaction( SIGPIPE, &sa, 0 );

    int acceptFd = *(int*)pAcceptFd;
    add_socket_thread(&socket_thread_table,pthread_self(),acceptFd);
    int contentLength=0;
    char bufHead1[] = "HTTP/1.1 200 OK\r\n"
            "Connection: keep-alive\r\n"
            "Content-Length: ";
    char bufHead2[] = "\r\n"
            "Content-Type: ";
    char bufHead3[] = "\r\n"
            "Date: Sun, 13 Sep 2015 17:11:27 GMT\r\n"
            "\r\n";
    char bufError[BUFLEN],bufRecv[BUFLEN_RECV];
    char contentLengthStr[5],buf[BUFLEN];

    int fp=0,err,recvBytes;
    request * req = NULL;
    char * const web_dir = "/www";
    char *file_path = (char*)malloc(sizeof(char)*2000);
    strcpy(file_path,web_dir);
    int web_dir_len = strlen(web_dir);
    char * contentType = (char*)malloc(sizeof(char)*50);
    memset(contentType,'\0',30);
    while (1){

        recvBytes = recv(acceptFd,bufRecv,BUFLEN_RECV,0);
        if(recvBytes > 0){
            printf("recv success\n");
            printf("recv string is %s\n",bufRecv);
        } else {
            printf("recv error : %s\n",strerror(errno));
            pipe_handler(0);
            return 0;
        }
        bufRecv[recvBytes] = '\0';
        req = request_init(bufRecv);

        //send html file
        *(file_path+web_dir_len) = '\0';
        strcat(file_path,req->url);
        if((fp = open(file_path,O_RDONLY)) == -1){
            sprintf(bufError,"error : %s\n",strerror(errno));

            send(acceptFd,bufHead1,strlen(bufHead1),0);
            contentLength = strlen(bufError);
            snprintf(contentLengthStr,10,"%d",contentLength);
            send(acceptFd,contentLengthStr,strlen(contentLengthStr),0);
            send(acceptFd,bufHead2,strlen(bufHead2),0);
            send(acceptFd,"text/plain",10,0);
            send(acceptFd,bufHead3,strlen(bufHead3),0);

            send(acceptFd,bufError,contentLength,0);

            printf("%s\n",bufError);
        } else{
            //send head
            send(acceptFd,bufHead1,strlen(bufHead1),0);
            snprintf(contentLengthStr,10,"%d",file_size(file_path));
            send(acceptFd,contentLengthStr,strlen(contentLengthStr),0);
            send(acceptFd,bufHead2,strlen(bufHead2),0);
            response_content_type(file_path,contentType);
            send(acceptFd,contentType,strlen(contentType),0);
            send(acceptFd,bufHead3,strlen(bufHead3),0);
            printf("send head success\n");

            //send file content http body
            ssize_t readSize;
            contentLength = 0;
            int count = 0;
            while( (readSize=read(fp,buf,BUFLEN)) > 0){
                contentLength += readSize;
                send(acceptFd,buf,readSize,0);
            }

            printf("send body success\n");
            close(fp);
            printf("close accept filepoint\n");
        }

        request_free(req);

    }//end of while loop
}
